﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HHPS.CreditEducation.DataProvider;
using HHPS.CreditEducation.Frameworks.Commons;

namespace HHPS.CreditEducation.Presentation.StudyPlans
{
    public partial class FormSelectSubject : HHControls.CreditEducation.HHSubForm
    {
        private readonly Business.Business _business;
        private readonly StudyCourse _studyCourse;

        public delegate void SelectedSubjectDelegate(List<Subject> subject);

        public event SelectedSubjectDelegate SelectedSubject;

        public FormSelectSubject(StudyCourse studyCourse)
        {
            InitializeComponent();

            _business = MainView.Instance.CreditEducationBusiness;
            _studyCourse = studyCourse;

            LoadSubjectList();
        }

        private void BtnSelectClick(object sender, EventArgs e)
        {
            if (dgvSubjectList.RowCount<1)
            {
                MessageBox.Show("Bạn đã chọn hết tất cả các môn học");
                Dispose();
            }

            var subjects = new List<Subject>();
            for (var i = 0; i < dgvSubjectList.RowCount; i++)
            {
                if (!(bool)dgvSubjectList[(string)typeof(Subject).Properties().IsChecked, i].Value) continue;

                subjects.Add(new Subject
                                 {
                                     Id = (int) dgvSubjectList[(string) typeof (Subject).Properties().Id, i].Value,
                                     Name = dgvSubjectList[(string) typeof (Subject).Properties().Name, i].Value.ToString()
                                 });
            }

            // Add subject to course
            if (SelectedSubject != null)
            {
                SelectedSubject(subjects);
            }

            LoadSubjectList();
        }

        private void LoadSubjectList()
        {
            dgvSubjectList.DataSource = _business.LoadSubjectByStudyCourse(_studyCourse);
            DataGridViewHelper.ShowHideColumns(dgvSubjectList, new[]
                                                                   {
                                                                       (string) typeof (Subject).Properties().IsChecked,
                                                                       (string) typeof (Subject).Properties().Name,
                                                                       (string)typeof(Subject).Properties().NumberOfCredit
                                                                   });
            DataGridViewHelper.ShowIsCheck(dgvSubjectList);

            dgvSubjectList.Columns[(string)typeof(Subject).Properties().IsChecked].HeaderText = string.Empty;
            dgvSubjectList.Columns[(string)typeof(Subject).Properties().NumberOfCredit].HeaderText = "Số tín chỉ";
            dgvSubjectList.Columns[(string)typeof(Subject).Properties().Name].HeaderText = "Môn học";
        }

        private void dgvSubjectList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvSubjectList == null ||
                dgvSubjectList.Columns[(string)typeof(Faculty).Properties().IsChecked] == null ||
                dgvSubjectList.Columns[(string)typeof(Faculty).Properties().IsChecked].Index != e.ColumnIndex)
                return;

            dgvSubjectList[e.ColumnIndex, e.RowIndex].Value =
                !(bool)dgvSubjectList[e.ColumnIndex, e.RowIndex].Value;
        }
    }
}
